package com.tuocent.dagv2.controller;

import com.tuocent.dagv2.common.AjaxResult;
import com.tuocent.dagv2.common.entity.LoginUser;
import com.tuocent.dagv2.common.entity.LoginUserInfo;
import com.tuocent.dagv2.common.utils.redis.RedisCache;
import com.tuocent.dagv2.constant.PublicConst;
import com.tuocent.dagv2.entity.ArchivesImport;
import com.tuocent.dagv2.service.IArchivesImportLogService;
import com.tuocent.dagv2.service.IArchivesImportService;
import com.tuocent.dagv2.service.IArchivesLogsService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * <p>
 * 数据导入信息 前端控制器
 * </p>
 *
 * @author Vincent
 * @since 2025-09-12
 */
@Controller
@RequestMapping("import")
public class ArchivesImportController {
    @Resource
    private RedisCache redis;
    @Autowired
    private IArchivesImportService archivesImportService;
    @Autowired
    private IArchivesImportLogService archivesImportLogService;

    @GetMapping("archivesImport")
    public String archivesImport(Model model) {
        return archivesImportService.archivesImport(model);
    }

    @RequestMapping("getArchivesImportData")
    @ResponseBody
    public AjaxResult getArchivesImportData(@RequestBody Map<String, Object> params) {
        return archivesImportService.getArchivesImportData(params);
    }

    @GetMapping("getDataTemplate")
    public void getDataTemplate(HttpServletResponse response,
                                @RequestParam("pid") String pid,
                                @RequestParam("tid") String tid,
                                @RequestParam("model") String model) throws IOException {
        archivesImportService.getDataTemplate(response, pid, tid, model);
    }

    @RequestMapping("setArchivesImportData")
    public String setArchivesImportData(Model model,
                                        @RequestParam("pid") String pid,
                                        @RequestParam("tid") String tid,
                                        @RequestParam("id") String id,
                                        @RequestParam(value = "importModel", defaultValue = "1") String importModel) {
        return archivesImportService.setArchivesImportData(model, pid, tid, id, importModel);
    }

    @PostMapping("ImportArchivesFile")
    @ResponseBody
    public AjaxResult ImportArchivesFile(HttpSession session,
                                         @RequestParam(value = "model", defaultValue = "0") Integer model,
                                         @RequestParam("pid") String pid,
                                         @RequestParam("tid") String tid,
                                         @RequestParam(value = "batchCode", defaultValue = "") String batchCode,
                                         @RequestParam("file") MultipartFile file) {
        return archivesImportService.ImportArchivesFile(session, pid, tid, model, batchCode, file);
    }

    @RequestMapping("delArchivesImport")
    @ResponseBody
    public AjaxResult delArchivesImport(@RequestParam("id") String id) {
        return archivesImportService.delArchivesImport(id);
    }

    @RequestMapping("archivesImportExecute")
    @ResponseBody
    public AjaxResult archivesImportExecute(HttpSession session, @RequestParam(value = "id", defaultValue = "") String id) throws IOException, IllegalAccessException, NoSuchFieldException, InterruptedException {
        if ("".equals(id)) {
            return AjaxResult.error("无效的数据参数");
        }
        LoginUserInfo loginUser = redis.get(session.getId());
        if (loginUser == null) {
            return AjaxResult.error("没有找到登录用户信息，请重新登录");
        }
        archivesImportService.archivesImportExecute(loginUser, id);
        return AjaxResult.success("导入程序已开始执行，请刷新状态获取最新的导入信息");
    }

    @RequestMapping("archivesImportLog")
    public String archivesImportLog(@RequestParam(value = "pid") String pid,
                                    Model model) {
        model.addAttribute("pid", pid);
        return "/import/archivesImportLog";
    }

    //数据导入日志列表
    @GetMapping("getArchivesImportLog")
    @ResponseBody
    public AjaxResult getArchivesImportLog(@RequestParam(value = "pid") String pid,
                                           @RequestParam(value = "page", defaultValue = PublicConst.DEF_PAGE_S) Integer page,
                                           @RequestParam(value = "limit", defaultValue = PublicConst.DEF_LIMIT_S) Integer limit) {
        return archivesImportLogService.getArchivesImportLog(pid, page, limit);
    }

    /**
     * 图像批量导入
     */
    @GetMapping("imagesImport")
    public String imagesImport(Model model) {
        return archivesImportService.imagesImport(model);
    }

    @RequestMapping("getImagesImportData")
    @ResponseBody
    public AjaxResult getImagesImportData(@RequestBody Map<String, Object> params) {
        return archivesImportService.getImagesImportData(params);
    }

    @RequestMapping("setImagesImportData")
    public String setImagesImportData(Model model,
                                      @RequestParam("pid") String pid,
                                      @RequestParam("tid") String tid,
                                      @RequestParam("id") String id,
                                      @RequestParam("classifyId") String classifyId,
                                      @RequestParam("classifyModel") String classifyModel) {
        return archivesImportService.setImagesImportData(model, pid, tid, id, classifyId, classifyModel);
    }

    @PostMapping("saveImagesImportTask")
    @ResponseBody
    public AjaxResult saveImagesImportTask(HttpSession session,
                                           @RequestBody ArchivesImport data) {
        return archivesImportService.saveImagesImportTask(session, data);
    }

    @PostMapping("ImagesImportExecute")
    @ResponseBody
    public AjaxResult ImagesImportExecute(HttpSession session,
                                          @RequestBody Map<String, Object> params) throws IOException {
        return archivesImportService.ImagesImportExecute(session, params);
    }
}
